<!-- 角色接口分配弹窗 -->
<template>
  <a-modal
    :width="460"
    title="分配接口"
    :visible="visible"
    :confirm-loading="loading"
    @update:visible="updateVisible"
    @ok="save">
    <a-spin :spinning="resourceLoading">
      <div style="height: 60vh;" class="ele-scrollbar-hover">
        <button @click="collapseAll">全部收起</button>
        <a-tree
          checkable
          :tree-data="resourceData"
          :replaceFields="{ children: 'children', title: 'nodeName', key: 'nodeId', value: 'nodeId' }"
          v-model:expandedKeys="expandKeys"
          v-model:checkedKeys="checkedKeys"/>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import {SysResourceApi} from '@/api/SysResourceApi';
import {SysRoleApi} from '@/api/SysRoleApi';


export default {
  name: 'SysRoleResource',
  emits: ['update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 当前角色数据
    data: Object
  },
  data() {
    return {
      // 资源数据
      resourceData: [],
      // 资源数据请求状态
      resourceLoading: false,
      // 提交状态
      loading: false,
      // 角色资源展开的keys
      expandKeys: [],
      // 角色资源选中的keys
      checkedKeys: [],
    };
  },
  watch: {
    visible() {
      if (this.visible) {
        this.query();
      }
    }
  },
  methods: {
    collapseAll() {
      this.expandKeys = [];
    },
    /**
     * 查询资源数据
     *
     * @author chenjinlong
     * @date 2021/4/7 11:00
     */
    query() {
      this.resourceData = [];
      this.expandKeys = [];
      this.checkedKeys = [];
      if (!this.data) {
        return;
      }
      this.resourceLoading = true;

      let roleId = this.data.roleId;
      SysResourceApi.getResourceList({roleId}).then(res => {
        this.resourceLoading = false;
        if (res.code === "00000") {
          let eks = [], cks = [];
          res.data.forEach(d => {
            eks.push(d.nodeId);
          });
          this.resourceData = this.$util.toTreeData(res.data, 'nodeId', 'nodeParentId');
          this.$util.eachTreeData(this.resourceData, (d) => {
            if (d.checked && (!d.children || !d.children.length)) {
              cks.push(d.nodeId);
            }
          });
          this.expandKeys = eks;
          this.checkedKeys = cks;
        } else {
          this.$message.error(res.message);
        }

      }).catch(e => {
        this.resourceLoading = false;
        this.$message.error(e.message);
      });
    },
    /**
     * 保存资源分配
     *
     * @author chenjinlong
     * @date 2021/4/7 11:00
     */
    save() {
      this.loading = true;
      let grantResourceList = [];
      this.$util.eachTreeData(this.resourceData, (d) => {
        if (this.checkedKeys.some(c => c === d.nodeId)) {
          grantResourceList.push(d.nodeId);
        }
      });
      SysRoleApi.grantResource({
        grantResourceList: grantResourceList,
        roleId: this.data.roleId
      }).then(res => {
        this.loading = false;
        if (res.code === '00000') {
          this.$message.success(res.message);
          this.updateVisible(false);
        } else {
          this.$message.error(res.message);
        }
      }).catch(e => {
        this.loading = false;
        this.$message.error(e.message);
      });
    },

    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
